package com.oly.cms.general.service.search;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.github.pagehelper.PageHelper;
import com.oly.cms.common.model.properties.OlyWebConfigProetries;
import com.oly.cms.general.mapper.WebSearchMapper;
import com.oly.cms.general.model.PageArticleTimeLine;
import com.oly.cms.general.model.param.WebArticleSearchParam;
import com.oly.cms.general.model.po.WebArticle;
import com.oly.cms.general.model.po.WebCat;
import com.oly.cms.general.model.po.WebTag;
import com.oly.cms.general.service.IWebSearchService;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.config.service.ISysConfigService;

import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class WebArticleServiceImpl implements IWebSearchService {
    @Autowired
    private WebSearchMapper webSearchMapper;

    @Autowired
    private ISysConfigService configService;

    /**
     * 综合查询
     * 
     * @param bb
     * @return
     */
    public List<WebArticle> listWebArticles(WebArticleSearchParam bb, String themeName) {
        if (StringUtils.isNotEmpty(themeName)) {
            setSupportType(bb, themeName);
        }
        return webSearchMapper.listWebArticlesBySearch(bb);
    }

    /**
     * 通过文章Id获取Html
     * 
     * @param articleId
     * @return
     */
    public WebArticle getWebArticleHtmlById(Long articleId) {

        return webSearchMapper.getWebArticleHtmlByArticleId(articleId);
    }

    /**
     * 通过文章Id获取Md
     * 
     * @param articleId
     * @return
     */
    public WebArticle getWebArticleMdById(Long articleId) {

        return webSearchMapper.getWebArticleHtmlByArticleId(articleId);
    }

    public WebArticle getWebArticleByUrl(String articleUrl) {

        return webSearchMapper.getWebArticleByArticleUrl(articleUrl);
    }

    public List<WebArticle> listWebArticlesOrder(Integer size, String order, String themeName) {
        return this.listWebArticlesOrder(1, size, order, themeName);
    }

    public List<WebArticle> listWebArticlesOrder(Integer size, Integer num, String order, String themeName) {
        WebArticleSearchParam ba = new WebArticleSearchParam();
        PageHelper.startPage(num, size, order);
        return this.listWebArticles(ba, themeName);
    }

    public List<WebArticle> listWebArticlesByCatId(Long catId, Integer size, String themeName) {
        WebCat webCat = webSearchMapper.getWebCatByCatId(catId);
        String supportType = getSupportType(themeName);
        System.out.println(supportType.split(",")[0]);
        if (webCat != null && (StringUtils.isEmpty(supportType)
                || ArrayUtils.contains(supportType.split(","), webCat.getCatType().toString()))) {
            WebArticleSearchParam webArticleSearchParam = new WebArticleSearchParam();
            webArticleSearchParam.setCatId(catId);
            PageHelper.startPage(1, size);
            return this.listWebArticles(webArticleSearchParam, null);
        } else {
            return null;
        }
    }

    public List<WebArticle> listWebArticlesByTagId(Long tagId, Integer size, String themeName) {
        WebTag webTag = webSearchMapper.getWebTagByTagId(tagId);
        String supportType = getSupportType(themeName);
        if (webTag != null && (StringUtils.isEmpty(supportType)
                || ArrayUtils.contains(supportType.split(","), webTag.getTagType().toString()))) {
            WebArticleSearchParam webArticleSearchParam = new WebArticleSearchParam();
            webArticleSearchParam.setTagId(tagId);
            PageHelper.startPage(1, size);
            return this.listWebArticles(webArticleSearchParam, null);
        } else {
            return null;
        }
    }

    public List<WebArticle> listWebArticlesByType(Byte type, Integer size, String orderString) {
        WebArticleSearchParam bb = new WebArticleSearchParam();
        bb.setArticleType(type);
        PageHelper.startPage(1, size, orderString);
        return this.listWebArticles(bb, null);
    }

    public PageArticleTimeLine groupByTime(Integer pageNum, Integer pageSize, WebArticleSearchParam bb,
            String themeName) {
        PageHelper.startPage(pageNum, pageSize, "create_time desc");
        List<WebArticle> list = this.listWebArticles(bb, themeName);
        // 按照时间分组
        Map<String, List<WebArticle>> map = list.stream()
                .collect(Collectors.groupingBy(webArticle -> neData(webArticle.getCreateTime())));
        return PageArticleTimeLine.getData(list, map);
    }

    public PageArticleTimeLine groupByTime(Integer pageNum, Integer pageSize, String themeName) {
        WebArticleSearchParam bb = new WebArticleSearchParam();
        return this.groupByTime(pageNum, pageSize, bb, themeName);
    }

    private String getSupportType(String themeName) {
        if (StringUtils.isEmpty(themeName)) {
            return null;
        }
        return configService.selectConfigDefauleValue(themeName, OlyWebConfigProetries.ARTICLE_TYPES);
    }

    public boolean allowComment(Long postId) {
        return webSearchMapper.allowComment(postId);
    }

    private void setSupportType(WebArticleSearchParam bb, String themeName) {
        if (StringUtils.isEmpty(themeName)) {
            return;
        }
        String supportType = configService.selectConfigDefauleValue(themeName, OlyWebConfigProetries.ARTICLE_TYPES);
        if (StringUtils.isNotEmpty(supportType)) {
            bb.setTypes(supportType);
        }
    }

    private String neData(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy MM");
        String newDate = sdf.format(date);
        return newDate;
    }
}
